/**
 * 管理初始化
 */
var Pan = {
    id: "PanTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    folderId:0,
};
/**
 * 初始化表格的列
 */
Pan.initColumn = function () {
    return [
        {field: 'selectItem', checkbox: true},
        {title: '文件名称', field: 'fileName', align: 'center', valign: 'middle', sortable: false},
        {title: '文件类型', field: 'fileType', align: 'center', valign: 'middle', sortable: false},
        {title: '文件大小', field: 'fileSize', align: 'center', valign: 'middle', sortable: true},
        {title: '文件来源', field: 'fileSource', align: 'center', valign: 'middle', sortable: false},
    ];
};

/**
 * 检查是否选中
 */
Pan.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        var fileIds;
        for (var i = 0; i < selected.length; i++) {
            if(i==0){
                fileIds = selected[i].fileId;
            }else{
                fileIds += ","+selected[i].fileId;
            }
        }
        Pan.seItem = fileIds;
        return true;
    }
};


/**
 * 删除
 */
Pan.del = function () {
    if (this.check()) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/pan/file/delete", function (data) {
	            Sys.success("删除成功!");
                Pan.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("fileIds",Pan.seItem);
	        ajax.start();
	    };
	    Sys.confirm("是否删除?",operation);
    }
};
/**
 * 批量下载
 */
Pan.download = function () {
    if (this.check()) {
        Sys.download(Sys.backendPath + "/pan/file/downbyfileid",{fileIds:Pan.seItem});
    }
};
/**
 * 转发
 */
Pan.forward = function () {
    if (this.check()) {
        Sys.addTabPage("转发",Sys.backendPath  + '/oa/msg/message/fileforward?personalFileId='+ Pan.seItem );
    }
};
/**
 * 查询列表
 */
Pan.search = function () {
    var queryData = {};
    queryData['fileName'] = $("#fileName").val();
    queryData['startDate'] = $("#startDate").val();
    queryData['endDate'] = $("#endDate").val();
    queryData['folderId'] = this.folderId;
    Pan.table.refresh({query: queryData});
};
Pan.onClickFolder = function (e, treeId, treeNode) {
    Pan.folderId = treeNode.id;
    Pan.search();
};
Pan.getFolderId = function(){
	return Pan.folderId;
}
/**
 * 点击添加文件夹
 */
Pan.addFolder = function () {
    var index = layer.open({
        type: 2,
        title: '新增文件夹',
        area: ['500px', '200px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Sys.backendPath + '/pan/folder/to_add'
    });
    this.layerIndex = index;
};

/**
 * 修改文件夹
 */
Pan.editFolder = function () {
    if (Pan.folderId) {
        var index = layer.open({
            type: 2,
            title: '修改文件夹',
            area: ['500px', '200px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Sys.backendPath + '/pan/folder/to_update/' + Pan.folderId
        });
        this.layerIndex = index;
    }else{
        Sys.info("默认目录不可选！");
    }
};

/**
 * 删除文件夹
 */
Pan.deleteFolder = function () {
    if (Pan.folderId) {
        var operation = function(){
            var ajax = new $ax(Sys.backendPath + "/pan/folder/delete", function (data) {
                Sys.success("删除成功!");
                Pan.table.refresh();
                Pan.ztree.init();
            }, function (data) {
                Sys.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("folderId",Pan.folderId);
            ajax.start();
        };
        Sys.confirm("是否删除该文件夹?",operation);
    }else{
        Sys.info("默认目录不可选！");
    }
};


/**
 * 显示文件夹的树
 * @param obj
 */
Pan.folderSelectTree = function (obj) {
    if(Pan.check()) {
        var _text_id = $(obj).attr("id");
        var _value_id = $(obj).attr("id") + "_id";
        var ids = $("#" + _value_id).val();
        var isMulti = false;
        var opt = {
            type: 2,
            title: '选择移动的文件夹',
            area: ['300px', '450px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: '/static/common/_selecttree.html',
            success: function (layero, index) {
                var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                iframeWin.initTree(Sys.backendPath + "/pan/folder/tree", isMulti, ids);
            }
        }
        opt["btn"] = [];
        opt.btn.push('<i class="fa fa-check"></i> 确定');
        opt['btn' + opt.btn.length] = function (index, layero) {
            var win = window[layero.find('iframe')[0]['name']];
            win.$('#keyword').val('').change();
            var codes = [], names = [], nodes;
            if (isMulti) {
                nodes = win.tree.getCheckedNodes(true);
            } else {
                nodes = win.tree.getSelectedNodes();
            }
            for (var i = 0; i < nodes.length; i++) {
                var folderId = nodes[i].id;
                var operation = function () {
                    var ajax = new $ax(Sys.backendPath + "/pan/file/move", function (data) {
                        Sys.success("转移成功!");
                        Pan.table.refresh();
                        Pan.ztree.init();
                        layer.close(index);
                    }, function (data) {
                        Sys.error("转移成功!" + data.responseJSON.message + "!");
                    });
                    ajax.set("fileIds", Pan.seItem);
                    ajax.set("folderId", folderId);
                    ajax.start();
                };
                Sys.confirm("确认转移？", operation);
            }
            $("#" + _value_id).val(codes.join(',')).change();
            $("#" + _text_id).val(names.join(',')).change();
        };


        opt.btn.push('<i class="fa fa-close"></i> 关闭');
        opt['btn' + opt.btn.length] = function (index, layero) {
            var index = layer.getFrameIndex(layero.find('iframe')[0]['name']); //获取窗口索引
            layer.close(index);
        };

        layer.open(opt);
    }
};

$(function () {
    var defaultColunms = Pan.initColumn();
    var table = new BSTable(Pan.id, Sys.backendPath + "/pan/file/list", defaultColunms);
    table.setPaginationType("server");
    table.setSubHeight(-5);
    Pan.table = table.init();
    var ztree = new $ZTree("folderTree", Sys.backendPath + "/pan/folder/tree");
    ztree.bindOnClick(Pan.onClickFolder);
    ztree.init();
    Pan.ztree = ztree;
});
